package com.shqcjd.yunweiyanoperator.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.pojo.ExpertDetailBean;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 用户详情页
 */
public class UserInfoActivity extends BaseActivity {

    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_staff_info_tag)
    TextView tvStaffInfoTag;
    @BindView(R.id.tv_staff_info_name)
    TextView tvStaffInfoName;
    @BindView(R.id.tv_staff_info_phone)
    TextView tvStaffInfoPhone;
    @BindView(R.id.tv_staff_info_company)
    TextView tvStaffInfoCompany;
    @BindView(R.id.tv_staff_info_info)
    TextView tvStaffInfoInfo;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.rl_staff_info_bottom)
    RelativeLayout rlStaffInfoBottom;
    @BindView(R.id.flowlayout_specialty)
    TagFlowLayout flowlayout_specialty;
    @BindView(R.id.tv_department)
    TextView tv_department;

    ArrayList<String> mTypeVals = new ArrayList<>();
    private Context mContext;
    private String mEmployeeId;
    private int isReview;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_user_info;
    }

    @Override
    public void init() {
        super.init();
        mContext = this;
    }

    @Override
    public void initView() {
        super.initView();
        String name = getIntent().getStringExtra("name");
        String companyName = getIntent().getStringExtra("companyName");
        String phone = getIntent().getStringExtra("phone");
        mEmployeeId = getIntent().getIntExtra("employeeId", 0)+"";
        //员工详情页传0  员工管理页传1
        isReview = getIntent().getIntExtra("isReview", 0);
        if(isReview==0){
            tvTitle.setText("员工详情");
            rlStaffInfoBottom.setVisibility(View.INVISIBLE);
        }else{
            tvTitle.setText("员工管理");
            rlStaffInfoBottom.setVisibility(View.VISIBLE);
        }
        tvStaffInfoTag.setText(name.substring(0,1));
        tvStaffInfoName.setText(name);
        tvStaffInfoPhone.setText(phone);
        tvStaffInfoCompany.setText("公司名称:  " + companyName);

        getUserDetail();
    }


    /**
     * 显示专业tag
     */
    private void initTag() {
        TagAdapter tagAdapter = new TagAdapter<String>(mTypeVals.toArray(new String[mTypeVals.size()])) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.tv_my_major_tag,
                        flowlayout_specialty, false);
                tv.setText(s);
                return tv;
            }

        };
        flowlayout_specialty.setAdapter(tagAdapter);
    }

    private void getUserDetail(){
        Map<String, String> map = new HashMap<>();
        map.put("userId",mEmployeeId);
        ApiService.getUserDetail(new JSONObject(map).toString())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ExpertDetailBean>>() {
                    @Override
                    public void _onNext(ResponseData<ExpertDetailBean> retData) {
                        if (retData.retCode == 10000) {
                            if (retData.data.departmentName != null) {
                                tv_department.setVisibility(View.VISIBLE);
                                tv_department.setText("部门:" + retData.data.departmentName);
                            } else {
                                tv_department.setVisibility(View.GONE);
                            }


                            if (retData.data.major != null && retData.data.major.size() > 0) {
                                mTypeVals.clear();
                                for (ExpertDetailBean.MajorBean majorBean : retData.data.major) {
                                    if (majorBean!=null)
                                    mTypeVals.add(majorBean.name);
                                }
                            }
                            initTag();

                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {

                    }
                });
    }


    private void staffReviewHttp(String review) {
        Map<String, String> map = new HashMap<>();
        map.put("employeeId", mEmployeeId);
        map.put("review", review);//1:批准,2:拒绝
        ApiService.staffReview(new JSONObject(map).toString())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("请稍等...");
                    }

                    @Override
                    public void _onNext(ResponseData responseData) {
                        if (responseData.retCode == 10000) {
                            ToastUtils.showShort(mContext, "操作成功");
                            finish();
                        } else {
                            ToastUtils.showShort(mContext, responseData.retMsg);
                        }
                        hideWaitingDialog();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    @OnClick({R.id.btn_finish, R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                finish();
                break;
            case R.id.tv_cancel:
                staffReviewHttp("2");
                break;
            case R.id.tv_confirm:
                staffReviewHttp("1");
                break;
        }
    }
}